package com.iv.durian.cloud.api;

import com.iv.durian.cloud.User;
import com.iv.durian.cloud.api.service.UserService;
import com.iv.durian.cloud.vo.AccessToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@Api(value = "AuthorityApi" ,description = "AuthorityApi",tags={"user"} )
public class AuthorityApi {

    @Autowired
    private  UserService userService;

    @ApiOperation(value = "获取access_token")
    @RequestMapping(value = "/access_token", method = RequestMethod.POST)
    public AccessToken token(
            @ApiParam(name = "mobileOrEmail",value = "用户标识",required = true) @RequestParam(value = "mobileOrEmail") String mobileOrEmail,
            @ApiParam(name = "password",value = "密码",required = true) @RequestParam(value = "password") String password
        ){
        User user =userService.getUserByMobileOrEmail(mobileOrEmail);
        Assert.notNull(user,"用户不存在");
        Assert.isTrue(userService.verifyPassword(user,password),"密码或用户不正确");
        String accessToken = userService.generateAccessToken(user);
        long expire = new Date().getTime() + TimeUnit.DAYS.toMillis(15);
        userService.updateTicket(user.getId(),accessToken,expire);
        AccessToken authorizeToken = AccessToken
                    .builder()
                    .accessToken(accessToken)
                    .expire(expire)
                    .build();
            return authorizeToken;

    }


    @ApiOperation(value = "注销当前登录帐户")
    @RequestMapping(value = "/api/logout", method = RequestMethod.GET)
    public void logout(
            @ApiParam(name = "user",value = "用户标识",required = true) @RequestParam(value = "user") String mobileOrEmail
    ){
        User user =userService.getUserByMobileOrEmail(mobileOrEmail);
        Assert.notNull(user,"用户不存在");
        userService.cleanTicket(user.getId());
    }


    @ApiOperation(value = "忘记密码")
    @RequestMapping(value = "/api/password", method = RequestMethod.POST)
    public void password(
            @ApiParam(name = "user",value = "用户标识",required = true) @RequestParam(value = "user") String mobileOrEmail,
            @ApiParam(name = "code",value = "验证码",required = true) @RequestParam(value = "code") String code,
            @ApiParam(name = "password",value = "新密码",required = true) @RequestParam(value = "password") String password
    ){
        Assert.isTrue(userService.verifyCode(mobileOrEmail,code),"验证码错误");
        User user = userService.getUserByMobileOrEmail(mobileOrEmail);
        Assert.notNull(user,"用户不存在");
        userService.updatePassword(user.getId(),password);
    }

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/registry", method = RequestMethod.POST)
    public void registry(
            @ApiParam(name = "user",value = "用户标识",required = true) @RequestParam(value = "user") String mobileOrEmail,
            @ApiParam(name = "code",value = "验证码",required = true) @RequestParam(value = "code") String code,
            @ApiParam(name = "password",value = "密码",required = true) @RequestParam(value = "password") String password
    ){
        Assert.isTrue(userService.verifyCode(mobileOrEmail,code),"验证码错误");
        User user = userService.getUserByMobileOrEmail(mobileOrEmail);
        Assert.isNull(user,"用户已存在");
        if (mobileOrEmail.contains("@")) {
            user.setEmail(mobileOrEmail);
        }else {
            user.setMobile(mobileOrEmail);
        }
        user.setPassword(password);
        userService.addUser(user);
    }


    @ApiOperation(value = "注册验证码")
    @RequestMapping(value = "/generate/code", method = RequestMethod.GET)
    public String generateCode(
            @ApiParam(name = "user",value = "用户标识",required = true) @RequestParam(value = "user")  String userKey
    ){
        User user = userService.getUserByMobileOrEmail(userKey);
        Assert.isNull(user, String.format("%s 已存在用户",userKey));
        return userService.generateCode(userKey);
    }

}
